package com.example.logintest.controller;

import com.example.logintest.bean.User;
import com.example.logintest.dao.AddUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/test")
@CrossOrigin(origins = "*",maxAge = 3600)
public class AddUserController {


    @Autowired
    AddUser addUser;

    @RequestMapping("/adduser")
    public Object addUser(@RequestParam(value = "username")String username,
                          @RequestParam(value = "password")String password){
        Map<String,Object> map =new HashMap<>();

        if(username==null||password==null){
            map.put("code",100);
            map.put("msg", "can not be empty");
        }else{
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            int code= addUser.addUser(user);
            if(code==1){
                map.put("code",200);
                map.put("msg", "success!");
            }else {
                map.put("code",100);
                map.put("msg", "fail!");
            }

        }
        return map;
    }
}
